package com.wangyi.fightimg.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wangyi.fightimg.R;

import java.lang.reflect.Field;

/**
 * @author wangYi
 *         AppCompatActivity页面基类
 */
public abstract class BaseFragmentActivity extends FragmentActivity implements View.OnClickListener {
    PercentRelativeLayout prlTitleRoot;// 标题栏
    TextView tvLeft;// 左侧内容
    TextView tvTitle;// 标题
    TextView tvRight;// 右侧内容
    FrameLayout flBody;// 主页面

    protected Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(R.layout.activity_base);
        // 处理本页面
        superFindView();
        supperAddListener();
        superInitealizedPage();
        // 处理子页面
        addLayout();
        findView();
        addListener();
        initializedPage();
    }

    /**
     * 初始化本类控件
     */
    private void superFindView() {
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvRight = (TextView) findViewById(R.id.tv_right);
        prlTitleRoot = (PercentRelativeLayout) findViewById(R.id.prl_title_root);
        flBody = (FrameLayout) findViewById(R.id.fl_body);
    }

    /**
     * 添加本类监听
     */
    private void supperAddListener() {
    }

    private void superInitealizedPage() {
        context = this;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //当前手机版本为5.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#3F51B5"));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
            TextView textView = new TextView(this);
            textView.setBackgroundResource(R.color.colorPrimary);// statusbar_bg是一个int型的颜色资源。
            textView.setLayoutParams(layoutParams);
            viewGroup.addView(textView);
        }
    }

    /**
     * 加载子页面
     */
    protected abstract void addLayout();

    /**
     * 初始化子页面控件
     */
    protected abstract void findView();

    /**
     * 添加监听
     */
    protected abstract void addListener();

    /**
     * 初始化页面
     */
    protected abstract void initializedPage();

    /**
     * 隐藏标题栏
     *
     * @param isGone 是否占据空间
     */
    protected void hideBar(boolean isGone) {
        if (isGone) {
            prlTitleRoot.setVisibility(View.GONE);
        } else {
            prlTitleRoot.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 隐藏左侧内容
     *
     * @param isGone 是否占据空间
     */
    protected void hideLeft(boolean isGone) {
        if (isGone) {
            tvLeft.setVisibility(View.GONE);
        } else {
            tvLeft.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 隐藏标题
     *
     * @param isGone 是否占据空间
     */
    protected void hideTitle(boolean isGone) {
        if (isGone) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 隐藏右侧内容
     *
     * @param isGone 是否占据空间
     */
    protected void hideRight(boolean isGone) {
        if (isGone) {
            tvRight.setVisibility(View.GONE);
        } else {
            tvRight.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置左侧内容
     *
     * @param strRes
     */
    protected void setTextLeft(int strRes) {
        tvLeft.setText(strRes);
    }

    /**
     * 设置左侧内容
     *
     * @param str
     */
    protected void setTextLeft(String str) {
        tvLeft.setText(str);
    }

    /**
     * 设置标题内容
     *
     * @param strRes
     */
    protected void setTextTitle(int strRes) {
        tvLeft.setText(strRes);
    }

    /**
     * 设置标题内容
     *
     * @param str
     */
    protected void setTextTitle(String str) {
        tvLeft.setText(str);
    }

    /**
     * 设置右侧内容
     *
     * @param strRes
     */
    protected void setTextRight(int strRes) {
        tvLeft.setText(strRes);
    }

    /**
     * 设置右侧内容
     *
     * @param str
     */
    protected void setTextRight(String str) {
        tvLeft.setText(str);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        flBody.removeAllViews();
        flBody.addView(view);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

}
